package com.example.hospital_208.controller;

import com.example.hospital_208.po.Department;
import com.example.hospital_208.po.Doctor;
import com.example.hospital_208.service.DepartmentService;
import com.example.hospital_208.service.DoctorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/departments")
@Tag(name = "部门管理接口", description = "医院科室的增删改查操作")
public class DepartmentController {

    private final DepartmentService departmentService;
    private final DoctorService doctorService;

    public DepartmentController(DepartmentService departmentService, DoctorService doctorService) {
        this.departmentService = departmentService;
        this.doctorService = doctorService;
    }

    // 新增接口：根据子科室ID获取医生列表
    @GetMapping("/sub/doctors/{sub_department_id}")
    @Operation(summary = "根据子科室ID获取医生列表", description = "通过子科室ID获取对应的医生列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = Doctor.class))),
            @ApiResponse(responseCode = "404", description = "子科室不存在或没有医生")
    })
    public List<Doctor> getDoctorsBySubDepartment(
            @PathVariable("sub_department_id")
            @Parameter(description = "子科室ID", example = "14", required = true)
            Integer subDepartmentId) {
        return doctorService.listDoctorsBySubDepartment(subDepartmentId);
    }

    // 其他原有接口保持不变...
    @GetMapping("/main/id/{department_name}")
    @Operation(summary = "根据科室名称获取ID", description = "通过科室名称获取对应的ID")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(type = "integer"))),
            @ApiResponse(responseCode = "404", description = "科室不存在")
    })
    public Integer getDepartmentIdByName(
            @PathVariable("department_name")
            @Parameter(description = "科室名称", example = "内科", required = true)
            String departmentName) {
        return departmentService.getDepartmentIdByName(departmentName);
    }
    @GetMapping("/main/names")
    @Operation(summary = "获取所有一级科室名称", description = "获取所有主科室(parent_id为null)的名称列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(type = "array", example = "内科")))
    })
    public List<String> getMainDepartmentNames() {
        return departmentService.listMainDepartmentNames();
    }

    @GetMapping("/sub/names/{parent_id}")
    @Operation(summary = "根据父科室ID获取子科室名称", description = "通过父科室ID获取对应的子科室名称列表")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(type = "array", example = "神经内科"))),
            @ApiResponse(responseCode = "404", description = "父科室不存在或没有子科室")
    })
    public List<String> getSubDepartmentNames(
            @PathVariable("parent_id")
            @Parameter(description = "父科室ID", example = "1", required = true)
            Integer parentId) {
        return departmentService.listSubDepartmentNames(parentId);
    }

    @GetMapping
    @Operation(summary = "查询所有部门", description = "获取医院所有科室信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = Department.class)))
    })
    public List<Department> getAllDepartments() {
        return departmentService.listDepartments();
    }

    @GetMapping("/{department_id}")
    @Operation(summary = "根据ID查询部门", description = "通过部门ID获取详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = Department.class))),
            @ApiResponse(responseCode = "404", description = "部门不存在")
    })
    public Department getDepartment(@PathVariable("department_id")
                                    @Parameter(description = "部门ID（主键）", example = "1", required = true)
                                    Integer departmentId) {
        return departmentService.getDepartmentById(departmentId);
    }

    @PostMapping
    @Operation(summary = "新增部门", description = "向系统中添加新科室")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "添加成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误（如部门名称重复）")
    })
    public String addDepartment(@RequestBody
                                @Parameter(description = "部门实体对象",
                                        content = @Content(schema = @Schema(implementation = Department.class)))
                                Department department) {
        int result = departmentService.addDepartment(department);
        return result > 0 ? "部门添加成功" : "部门添加失败";
    }

    @PutMapping
    @Operation(summary = "修改部门信息", description = "更新已有部门的信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或部门不存在")
    })
    public String updateDepartment(@RequestBody
                                   @Parameter(description = "包含更新信息的部门实体",
                                           content = @Content(schema = @Schema(implementation = Department.class)))
                                   Department department) {
        int result = departmentService.editDepartment(department);
        return result > 0 ? "部门信息修改成功" : "部门信息修改失败";
    }

    @DeleteMapping("/{department_id}")
    @Operation(summary = "删除部门", description = "根据ID删除科室（若有子部门则无法删除）")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或部门存在关联数据")
    })
    public String deleteDepartment(@PathVariable("department_id")
                                   @Parameter(description = "待删除的部门ID", example = "14", required = true)
                                   Integer departmentId) {
        int result = departmentService.removeDepartment(departmentId);
        return result > 0 ? "部门删除成功" : "部门删除失败";
    }
}